package com.example.p99;

/**
 * @author Lenovo
 */
import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) {
        String serverAddress = "localhost";
        int port = 12345; // 服务器端口

        try (Socket socket = new Socket(serverAddress, port)) {
            System.out.println("已连接到服务器");

            OutputStream output = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(output, true);
            InputStream input = socket.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));

            String userInput;
            while ((userInput = consoleReader.readLine()) != null) {
                writer.println(userInput);
                String serverResponse = reader.readLine();
                System.out.println("服务器回应: " + serverResponse);
            }
        } catch (UnknownHostException e) {
            System.err.println("不明主机: " + serverAddress);
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("无法获取 I/O 连接到: " + serverAddress);
            e.printStackTrace();
        }
    }
}